<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Jobs\ArticleWeight;
use App\Models\Animal;
use App\Models\Article;
use App\Models\Circle;
use App\Models\Share;
use App\Services\BalanceService;
use Illuminate\Http\Request;

class ShareController extends BaseController
{

    public $model = Share::class;

    /**
     * 关注
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Throwable
     */
    public function store(Request $request) {
        $this->validate($request, [
            'entity_id' => 'required',
            'entity_type' => 'required',
        ]);
        $user_id = $this->getUserId();
        $input = $request->input();

        $input['user_id'] = $user_id ?: $request->header('fake_uid');
        if (!$input['user_id'])
            return $this->failed('操作失败，检查fake-uid');

        $entity = null;
        $entity_type = $input['entity_type'];
        if ($entity_type == Share::TYPE_ARTICLE)
            $entity = Article::find($input['entity_id']);
        else if($entity_type == Share::TYPE_ANIMAL)
            $entity = Animal::find($input['entity_id']);
        else if($entity_type == Share::TYPE_CIRCLE)
            $entity = Circle::find($input['entity_id']);
        else if ($entity_type == Share::TYPE_ANIMAL_CANVAS) {
            $entity = Animal::find($input['entity_id']);
        }

        if ($entity_type == Share::TYPE_ARTICLE  )  {
            if ($entity->user_id != $input['user_id']) {
                $one = Share::where('user_id', $input['user_id'])->where('entity_id', $input['entity_id'])->where('entity_type', $entity_type)->first();
                if (!$one) {
                    if ($input['user_id']) {
                        Share::create([
                            'user_id' => $input['user_id'],
                            'entity_id' => $input['entity_id'],
                            'entity_type' => $input['entity_type'],
                            'entity_user_id' => $entity->user_id
                        ]);
                    }
                    if ($user_id)
                        BalanceService::share($user_id);

                    $entity->share_num += 1;
                    $entity->save();

                    $this->dispatch(new ArticleWeight($entity->id));
                }
            }
        } else if(!in_array($entity_type,  [Share::TYPE_CHECK_PHOTO_TOOL, Share::TYPE_VOICE_TRANSLATE])) {
            if ($user_id)
                BalanceService::share($user_id);

            if ($entity) {
                $entity->share_num += 1;
                $entity->save();
            }
        } else {
            Share::create([
                'user_id' => $input['user_id'],
                'entity_id' => $request->input('entity_id', -1),
                'entity_type' => $input['entity_type'],
                'entity_user_id' => 0
            ]);
        }

        return $this->success([]);
    }

}

